package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.GanYu;
import com.itheima.service.TiZhiBianSHiService;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.security.SecureRandom;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("zhongyi")
public class TiZhiBianSHiController {
    @Reference(version = "1.0.0")
    private TiZhiBianSHiService tiZhiBianSHiService;

    @PostMapping("findAll")
    public Result findAll(@RequestBody QueryPageBean queryPageBean) throws ParseException {
        return tiZhiBianSHiService.findAll(queryPageBean);
    }

    @GetMapping("delete")
    public Result delete(Integer id) {
        return tiZhiBianSHiService.del(id);
    }

    @PostMapping("update")
    public Result uodate(@RequestBody GanYu ganYu) {
        return tiZhiBianSHiService.update(ganYu);
    }

    @PostMapping("getProblem")
    public Result getProblem() {
        return tiZhiBianSHiService.getProblem();
    }

    @PostMapping("add")
    public Result add(@RequestBody HashMap<String, Object> map) throws ParseException {
        List<String> list = (List<String>) map.get("daan");
        int sum = 0;
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse((String) map.get("date"));
        String name = (String) map.get("name");
        for (String s : list) {
            if ("A".equals(s)) {
                sum = sum + 1;
            } else if ("B".equals(s)) {
                sum = sum + 2;
            } else if ("C".equals(s)) {
                sum = sum + 3;
            } else {
                sum = sum + 4;
            }
        }
        String dang = new SimpleDateFormat("YYYY-MM-dd").format(new Date());
        dang += UUID.randomUUID().toString().substring(0, 5);
        GanYu ganYu = new GanYu(null, dang, name, date, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        if ((sum) / 9 > 80) {
            ganYu.setPinghe(1);
        } else if (sum % 3 == 0) {
            ganYu.setYinxu(1);
        } else if (sum % 3 == 1) {
            ganYu.setYinxu(1);
        } else {
            ganYu.setTebin(1);
        }
        return tiZhiBianSHiService.add(ganYu);
    }
}
